//
//  WYBOSSTeamController.m
//  Boss
//
//  Created by admin on 2018/7/3.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYBOSSTeamController.h"
#import "WYBOSSTeamSectionView.h"
#import "WYInviteTime.h"
#import "WYRedPacketListCell.h"
@interface WYBOSSTeamController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong) NSArray *thirtyTime;
@property (nonatomic,strong) NSArray *inviteNum;
@property (nonatomic,strong) NSString *totalInviteCount;
@property (nonatomic,strong) NSString *totalInviteMoney;
@end

@implementation WYBOSSTeamController
-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    // Do any additional setup after loading the view.
    [self loadData];
    
    [self setNoDataViweWithTitle:@"您还没有BOSS团哦～" img:[UIImage imageNamed:@"粉丝"]];
    
    
}
-(void)loadData
{
    WEAKSELF
    [WYHttpServer getBossTeamSucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        if (success) {
            NSArray *array =[WYInviteTime getInviteArrayFromJson:response[@"result"][@"invitePerson"]];
            if (array.count) {
                [weakSelf.dataArray addObject:array];
            }
            if (weakSelf.dataArray.count) {
                [weakSelf hiddenNoDataView];
            }else{
                [weakSelf showNoDataView];
            }
            weakSelf.thirtyTime = response[@"result"][@"thirtyTime"];
            weakSelf.inviteNum = response[@"result"][@"inviteNum"];
            weakSelf.totalInviteCount  = response[@"result"][@"totalInviteCount"];
            weakSelf.totalInviteMoney  = response[@"result"][@"totalInviteMoney"];
            [weakSelf.tableView reloadData];
        }else{
            if (weakSelf.dataArray.count) {
                [weakSelf hiddenNoDataView];
            }else{
                [weakSelf showNoDataView];
            }
            
        }
    }];
}
-(void)setupUI
{
    self.navigationItem.title = @"BOSS团";
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"WYRedPacketListCell" bundle:nil] forCellReuseIdentifier:@"WYRedPacketListCell"];
    self.tableView.estimatedRowHeight = 60;
     [self wr_setNavBarShadowImageHidden:YES];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataArray[section] count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     WYRedPacketListCell *cell =[tableView dequeueReusableCellWithIdentifier:@"WYRedPacketListCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"WYRedPacketListCell" owner:nil options:nil].firstObject;
    }
    
    WYInviteTime *time = self.dataArray[indexPath.section][indexPath.row];
    [cell setTime:time];
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    WYBOSSTeamSectionView *sectionView = [[WYBOSSTeamSectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 430)];
    sectionView.backgroundColor = [UIColor whiteColor];
    if (self.totalInviteCount) {
        sectionView.personLabel.text = [NSString stringWithFormat:@"%@人",self.totalInviteCount];
    }
    if (self.totalInviteMoney) {
         sectionView.moenyLabel.text = [NSString stringWithFormat:@"%.1f元",self.totalInviteMoney.floatValue/1000.0];
    }
    if (self.thirtyTime.count&&self.inviteNum.count) {
//        NSArray *xArray =[[self.thirtyTime reverseObjectEnumerator] allObjects];
//        NSArray *yArray =[[self.inviteNum reverseObjectEnumerator] allObjects];
        
        [sectionView setXArray:self.thirtyTime WithYArray:self.inviteNum];
    }
    
    
    return sectionView;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 430;
}


- (UITableView *)tableView
{
    if (_tableView == nil) {
        CGRect frame = CGRectMake(0, self.navHeight, kScreenWidth, kScreenHeight-self.navHeight);
        _tableView = [[UITableView alloc] initWithFrame:frame
                                                  style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
    }
    return _tableView;
}

@end
